package com.day4.system.dao;

import com.day4.system.domain.Teacher;

/**
 * @author 小坡
 * @date 2022/8/25  18:14
 **/
public class TeacherDao {

    private static Teacher[] teachers = new Teacher[5];  //  使得数据共享

    public boolean addTeacher(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("添加失败，装满了~");
            return false;
        } else {
            teachers[index] = teacher;
            return true;
        }
    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void delTeacher(String id) {
//        1、查询id在数组中的索引位置
        int index = getIndex(id);
//        2、将该索引位置的元素，使用null进行替换
        teachers[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String id, Teacher newTeacher) {
        int index = getIndex(id);
        teachers[index] = newTeacher;
    }
}
